<?php
/**
 * @Author: 杰少Pakey
 * @Email : admin@ptcms.com
 * @File  : picture.php
 */
 
class PictureController extends AdminController{
	public function init()
	{
		$this->module = 'picture';
		$this->module_id = 3;
		$this->typename = '图集';
		$this->tableName = 'picture';
		parent::init();
	}
	public function addAction()
	{
		$this->assign('actionname', '添加' . $this->typename);
		$this->assign('categorylist', M('category')->getModuleList($this->module_id));
		parent::addAction();
	}
	public function insertAction()
	{
		$data = array();
		$data['title'] = $_POST['title'];
		$data['category_id'] = $_POST['category_id'];
		$data['thumb'] = $_POST['thumb'];
		$data['pictureurls'] = $_POST['pictureurls'];
		$data['content'] = $_POST['content'];
		$data['keywords'] = $_POST['keywords'];
		$data['description'] = $_POST['description'];
		$data['weight'] = $_POST['weight'];
		$data['top'] = $_POST['top'];
		$data['status'] = $_POST['status'];
		$data['datetime'] = strtotime($_POST['datetime']);
		$data['urlkey'] = strtotime($_POST['datetime']);
		
		
		$res=$this->model->insert($data);
		if ($res){
			//var_dump($res);
			$article_data = array();
			$article_data['picture_id'] = $res;
			$article_data['content'] = $data['content'];
			$article_data['pictureurls'] = $data['pictureurls'];
			M('picture_data')->insert($article_data);
			$this->success('添加成功',cookie('admin_listurl'));
		}else{
			$this->error("添加失败[{$this->model->getLastSql()}]");
		}
	}
	public function editAction()
	{
		$id=I('get.id','en','');
		$info=$this->model->where(array($this->model->getPk()=>$id))->find();
		$info_data=M('picture_data')->where(array('picture_id'=>$id))->find();
		//var_dump($info);
		$info['content'] = $info_data['content'];
		$info['pictureurls'] = $info_data['pictureurls'];
		if ($info){
			$this->assign('categorylist', M('category')->getModuleList($this->module_id));
			$this->assign('actionname', '修改' . $this->typename);
			$this->assign('info',$info);
			$this->display();
		}else{
			$this->error('找不到指定的信息');
		}
	}
	public function updateAction()
	{
		$id=I('post.id','en','');
		$data = array();
		$data['title'] = $_POST['title'];
		$data['category_id'] = $_POST['category_id'];
		$data['thumb'] = $_POST['thumb'];
		$data['pictureurls'] = $_POST['pictureurls'];
		$data['content'] = $_POST['content'];
		$data['keywords'] = $_POST['keywords'];
		$data['description'] = $_POST['description'];
		$data['weight'] = $_POST['weight'];
		$data['top'] = $_POST['top'];
		$data['status'] = $_POST['status'];
		$data['datetime'] = strtotime($_POST['datetime']);
		$data['urlkey'] = strtotime($_POST['datetime']);
		$res=$this->model->where(array($this->model->getPk()=>$id))->update($data);
		if ($res===0 or $res===1){
			//$this->success('修改成功',cookie('admin_listurl'));
			$article_data = array();
			$article_data['content'] = $data['content'];
			$article_data['pictureurls'] = $data['pictureurls'];
			M('article_data')->where(array('article_id'=>$id))->update($article_data);
			$this->success('修改成功');
		}else{
			$this->error("修改失败[{$this->model->geterror()}]");
		}
	}
	public function getAddonData()
	{
		$downlinkurl=array();
		foreach($_POST['downlinkurl'] as $k=>$v){
			$downlinkurl[]=array(
				'type'=>$_POST['downlinktype'][$k],
				'url'=>$v
			);
		}
		return array(
			'content'=>$_POST['content'],
			'downlinkurl'=>serialize($downlinkurl),
			'downnum'=>$_POST['downnum'],
			'attachment'=>$_POST['attachment']
		);
	}
}